package com.net.xpay.core.manager.impl.auto;

import com.google.common.base.Preconditions;
import com.net.xpay.core.constant.TransBizTypeConstant;
import com.net.common.annotation.WriteTransactional;
import com.net.common.dao.BasicMapper;
import com.net.common.dao.Page;
import com.net.common.manager.AbstractVersionedManager;
import com.net.noah.core.service.SerialNumberService;
import com.net.xpay.core.dao.auto.AutoPayTransBatchMapper;
import com.net.xpay.core.domain.auto.AutoPayTransBatch;
import com.net.xpay.core.manager.auto.AutoPayTransBatchManager;
import com.net.xpay.core.model.AutoPayTransBatchSearchModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;

import java.util.List;


@Component
public class AutoPayTransBatchManagerImpl extends AbstractVersionedManager<AutoPayTransBatch> implements AutoPayTransBatchManager{

    @Autowired
    private AutoPayTransBatchMapper mapper;

    @Autowired
    private SerialNumberService serialNumberService;

    @Override
    @WriteTransactional(propagation = Propagation.SUPPORTS)
    public int save(AutoPayTransBatch obj) {
        Preconditions.checkArgument(obj.getId() == null);
        obj.setId(serialNumberService.getLongSerialNumber(getTableFullName()));
        obj.setBizId(TransBizTypeConstant.YT_AT + obj.getId());
        return super.save(obj);
    }

    @Override
    protected BasicMapper<Long, AutoPayTransBatch> getMapper() {
        return mapper;
    }

    @Override
    public AutoPayTransBatch findByBizId(Long userId, String bizId) {
        Preconditions.checkArgument(userId != null && bizId != null);
        AutoPayTransBatch condition = new AutoPayTransBatch();
        condition.setUserId(userId);
        condition.setBizId(bizId);
        return getByCondition(condition);
    }

    @Override
    public List<AutoPayTransBatch> search(AutoPayTransBatchSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        Page page = new Page(searchModel);
        page.addOrder("id", "desc");
        return nonEmptyList(mapper.search(searchModel, page));
    }

    @Override
    public long searchCount(AutoPayTransBatchSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.searchCount(searchModel);
    }

    @Override
    public AutoPayTransBatch findById(Long userId, Long id) {
        Preconditions.checkArgument(userId != null && id != null);
        AutoPayTransBatch condition = new AutoPayTransBatch();
        condition.setUserId(userId);
        condition.setId(id);
        return getByCondition(condition);
    }

    @Override
    public List<AutoPayTransBatch> list(AutoPayTransBatchSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return nonEmptyList(mapper.list(searchModel));
    }
}
